package com.metagurukul.metagranth.actionservlets;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.metagurukul.metagranth.service.UserService;
import com.metagurukul.metagranth.vo.User;

/**
 * This servlet is responsible for handling the log in operations.
 * @author team MetaGranth
 *
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
	
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}

	/**
	 * this function will get the username and password for log in process.
	 * authenticates the user weather the pair already exist in the database or not.
	 * if yes then the user is directed to home.jsp
	 * else wrong username password is shown on index.jsp
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doPerform(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("username") != null
				&& request.getParameter("password") != null) {
			try {
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			User user = new User();
			user.setUsername(username);
			user.setPassword(password);
			RequestDispatcher rd = null;
			
				User findUser = UserService.authentication(user);
				if (findUser != null) {
					int userId = findUser.getUid();
					int userType = findUser.getType();
					String userName = findUser.getUsername();
					HttpSession session = request.getSession(true);
					session.setAttribute("userId", userId);
					session.setAttribute("userName", userName);
					session.setAttribute("userType", userType);
					response.sendRedirect("home.jsp");
				} else {
					request.setAttribute("msg", "Wrong Username and Password");
					rd = request.getRequestDispatcher("index.jsp");
					rd.forward(request, response);
				}
				
			} catch (Exception exception) {
				request.setAttribute("errormsg", exception.getMessage());
				request.getRequestDispatcher("errorPage.jsp").forward(request, response);
			}
		} else {
			response.sendRedirect("index.jsp");
		}

	}

}
